'''

'''


import struct
from data_type_yizimi import *
from module_of_msg import *
import os

def byte_to_bits(x: int):
    '''
    return list: [int * 8]
    '''
    a = []
    
    

def read_bin(length: int, data: bytes):
    # f = open("openflow6.pcap", "rb")
    int_size = struct.calcsize("c")
    get_bin = []
    # print(int_size)
    for i in range(int(length / int_size)):
        # print(data[:int_size])
        (i,), data = struct.unpack("c", data[:int_size]), data[int_size:]
        # print(hex(ord(i)), data)
        get_bin.append(ord(i))
    return get_bin

    
            
if __name__ == "__main__":
    print(os.path.getsize("/home/yizimi/project/net-security-scalable-network-security-analysis-system/framework/openflow6.pcap"))
    length = os.path.getsize("/home/yizimi/project/net-security-scalable-network-security-analysis-system/framework/openflow6.pcap")
    f = open("/home/yizimi/project/net-security-scalable-network-security-analysis-system/framework/openflow6.pcap", "rb")
    pcapheader = 0
    data = f.read(length)
    # print(type(data))
    pm = Pcaph_mode
    ethm = EthII_mode
    pktm = Pkt_mode 
    # ph = Pcapheader(24)
    msg = read_bin(length, data)
    
    with open("data.txt", "w+") as file:
        y = 0
        for x in msg:
            file.write(hex(x))
            if y % 4 == 3:
                file.write("\n")
            else:
                file.write(" ")
            y = y + 1
                
    print(msg[:50])
    (ph, msg) = Matching_message(msg, [pm])
    ph.Print()
    # if ph.get_seg_data('Magic') != 0xd4c3b2a1:
        # BigOrSmall = 1
    # msg = msg[24: ]
    # 0000 0000 0000 0000 0000 0100 0000 0000
    # 0000 0000 0000 0100 0000 0000 0000 0000
    adder = 24
    while len(msg) != 0:
        # length, msg = read_packetheader(msg)
        packetheader, msg = Matching_message(msg, [pktm, ethm])
        packetheader.Print()
        # read_packetdata()
        adder += packetheader.msg_len
        print(packetheader.msg_len, adder)
        input()
    
    # ph.Print()